#include "d_Math.h"

using namespace d_Math;

d_Plane::d_Plane(d_Vec3f origin, d_Vec3f normal) {
  this->normal = normal;
  this->origin = origin;
  equation[0] = normal[0];
  equation[1] = normal[1];
  equation[2] = normal[2];
  equation[3] = -(normal[0]*origin[0]+normal[1]*origin[1]
                 +normal[2]*origin[2]);
}

d_Plane::d_Plane(d_Vec3f p1,d_Vec3f p2, d_Vec3f p3)
{
  normal = cross((p2-p1),(p3-p1));
  normal = norm(normal);
  origin = p1;
  equation[0]  = normal[0];
  equation[1]  = normal[1];
  equation[2]  = normal[2];
  equation[3]  = -(normal[0]*origin[0]+normal[1]*origin[1]
                 +normal[2]*origin[2]);
}
bool d_Plane::isFrontFacingTo(d_Vec3f direction)
{
  double d = dot(normal,direction);
  return (d <= 0);
}

double d_Plane::signedDistanceTo(d_Vec3f point)
{
  return dot(point,normal) + equation[3];
}

